package com.CMUBiS.DB;

import java.security.MessageDigest;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

import com.CMUBiS.util.Address;
import com.CMUBiS.util.Bid;
import com.CMUBiS.util.Category;
import com.CMUBiS.util.Image;
import com.CMUBiS.util.Item;
import com.CMUBiS.util.User;

public abstract class DBQuery {
	protected String url = null;
	protected String driver = null;
	protected String username = null; 
	protected String password = null;
	protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final Random generator = new Random();
	
	public void DBQuery(){
		this.init();
	}
	
	public int getRand() {
		return generator.nextInt();
	}
	
	protected String authToken(int id){
		// Create the auth token
		Date date = new Date();
		String result = null;
		String authToken = "auth:" + Math.abs(getRand()) + id + date.getTime();
			
		byte[] bytesOfMessage=null;
		MessageDigest md = null;
			
		try {
			bytesOfMessage = authToken.getBytes("UTF-8");
			md = MessageDigest.getInstance("MD5");
			
			byte[] thedigest = md.digest(bytesOfMessage);
			authToken = thedigest.toString();
			result = authToken;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	protected abstract void init();
	
	protected abstract Connection initConnection();
	
	protected abstract Connection getConnection();
	
	protected abstract void closeConnection(Connection conn);
	
	public abstract int checkUsernamePassword(String username, String password);
	
	public abstract String makeNewAuthToken(int userID);
	
	public abstract boolean checkAuthToken(int userID, String authToken);
	
	public abstract Date getItemEndDate(int itemID);
	
	public abstract float getCurrentBid(int itemID);
	
	public abstract float getMaxBid(int itemID);
	
	public abstract int getQuantity(int itemID);
	
	public abstract boolean insertAddress(int userID, String street, String town, String zip, int state, Boolean isDefault);
	
	public abstract boolean updateAddress(int id, int userID, String street, String town, String zip, int state, Boolean isDefault);
	
	public abstract boolean insertItem(int userID, String name, String description, float startPrice, float reservePrice, float buyNowPrice, int quantity, Date endDate, int categoryID);
	
	public abstract Item getItem(int itemID, Boolean getImages);
	
	public abstract float insertBid(int userID, int itemID, int quantity, float bid, float maxBid);
	
	public abstract boolean buyItemNow(int userID, int itemID, int quantity, int accountID);
	
	public abstract ArrayList<Image> getItemImages(int itemID);
	
	public abstract ArrayList<Item> getCurrentSellingItems(int userID);
	
	public abstract ArrayList<Item> getOldSellingItems(int userID);
	
	protected abstract ArrayList<Item> getXSellingItems(int userID, String table);
	
	public abstract ArrayList<Bid> getCurrentBids(int userID);
	
	public abstract ArrayList<Bid> getOldBids(int userID);
	
	public abstract ArrayList<Category> getAllCategories() throws Exception;
	
	public abstract ArrayList<Category> getCategories(int parent) throws Exception;
	
	
	public abstract ArrayList<Item> getCategoryItems(int categoryID, int page, int itemsPP) throws Exception;
	
	public abstract ArrayList<Item> getTextItems(String text);
	
	public abstract Category getCategory(int categoryID) throws Exception;
	
	public abstract String getFirstName(int userID);
	
	public abstract Address getDefaultAddress(int userID);
	
	public abstract ArrayList<Address> getAddresses(int userID);
	
	public abstract User getUser(int userID);
	
	public abstract boolean insertUser(String username, String password, String email, String firstName, String lastName);
	
	public abstract boolean updateUser(String userID,String username, String password, String email, String firstName, String lastName);
	
	public abstract boolean emailExists(String email);
	
	public abstract boolean usernameExists(String username);
	
	public abstract ArrayList<String[]> getStates() throws Exception;

	public abstract ArrayList<Bid> getCurrentBidsTime(int parseInt, Date parse);
	public abstract ArrayList<Item> getCurrentSellingItemsTime(int parseInt, Date parse);
	public abstract ArrayList<Bid> getOldBidsTime(int parseInt, Date parse);
	public abstract ArrayList<Item> getOldSellingItemsTime(int parseInt, Date parse);
}
